package com.newtouch.bxzs.database.synchronize.batch.writer;

import com.newtouch.bxzs.common.base.constant.RabbitMQConstant;
import com.newtouch.bxzs.common.base.em.AdvertisementTypeEnum;
import com.newtouch.bxzs.common.base.em.BusinessMessageEnum;
import com.newtouch.bxzs.common.base.em.PublishStatusEnum;
import com.newtouch.bxzs.common.base.model.*;
import com.newtouch.bxzs.common.base.model.bo.PushMessageBO;
import com.newtouch.bxzs.common.base.util.RabbitMQUtils;
import com.newtouch.bxzs.database.synchronize.mapper.BatchMapper;
import com.newtouch.bxzs.database.synchronize.model.AdvertisementBO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地批处理发布撤销
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/3/2 16:07</td>
 * <td>yuanjie</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author yuanjie
 * @version 1.0.0
 * @since 1.0.0
 */
@Component
@SuppressWarnings("unchecked")
public class WriteLocalData {

    @Autowired
    private BatchMapper batchMapper;

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * 根据广告id撤销广告
     * @return ItemWriter
     */
    public ItemWriter<AdvertisementBO> revokeAdvertisement() {
        return list -> batchMapper.updateAdvertisement((List<AdvertisementBO>) list);
    }

    /**
     * 根据广告id启动广告
     * @return ItemWriter
     */
    public ItemWriter<AdvertisementBO> publishAdvertisement() {
        return items -> {
            List<AdvertisementBO> advertisements = (List<AdvertisementBO>) items;
            //获取启动页和首页可启动的条数
            Integer startCount = 5-advertisements.get(0).getStartCount();
            Integer homeCount  = 5-advertisements.get(0).getHomeCount();
            List<AdvertisementBO> startAdvertisement = new ArrayList<>();
            List<AdvertisementBO> homeAdvertisement = new ArrayList<>();
            for (AdvertisementBO advertisement:advertisements){
                if (AdvertisementTypeEnum.START_ADVERTISEMENT.getCode().equals(advertisement.getAdvertisementType())){
                    //启动页
                    if (startAdvertisement.size() <= startCount){
                        startAdvertisement.add(advertisement);
                    }
                }else {
                    //首页
                    if (homeAdvertisement.size() <= homeCount){
                        homeAdvertisement.add(advertisement);
                    }
                }
            }
            startAdvertisement.addAll(homeAdvertisement);
            batchMapper.updateAdvertisement(startAdvertisement);
        };
    }

    /**
     * 根据id发布/撤销产品信息
     * @return ItemWriter
     */
    public ItemWriter<Product> updateProductStatus() {
        return list -> batchMapper.updateProduct((List<Product>) list);
    }

    /**
     * 根据id发布/撤销通知信息
     * @return ItemWriter
     */
    public ItemWriter<Notification> updateNotificationStatus() {
        return list -> batchMapper.updateNotification((List<Notification>) list);
    }

    /**
     * 根据id发布/撤销知识小灵通
     * @return ItemWriter
     */
    public ItemWriter<KnowledgeCard> updateKnowledgeCardStatus() {
        return list -> batchMapper.updateKnowledgeCard((List<KnowledgeCard>) list);
    }

    /**
     * 根据id发布/撤销知识小灵通
     * @return ItemWriter
     */
    public ItemWriter<KnowledgeBase> updateKnowledgeBaseStatus() {
        return list -> batchMapper.updateKnowledgeBase((List<KnowledgeBase>) list);
    }

    /**
     * 提醒升级
     * @return ItemWriter
     */
    public ItemWriter<User> remindToUpgrade() {
        return items -> {
            for (User user :items) {
                PushMessageBO message = PushMessageBO.builder().title("客户接收督办通知")
                        .content("领导,你好!业务员:"+ user.getUserName()+"(工号"+user.getUserJobNumber()+")有客户推送信息目前还未接收，请知晓处理!").build();
                if (StringUtils.isEmpty(user.getUserLeaderJobNumber())){
                    user.setUserLeaderJobNumber(user.getUserJobNumber());
                }
                rabbitTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_STR, RabbitMQConstant.MESSAGE_NOTICE,
                        RabbitMQUtils.createUserJobNumberRabbitMap(user.getUserLeaderJobNumber(), message,
                                BusinessMessageEnum.UPGRADE_MESSAGE_REMINDER.getResultCode()));
            }
        };
    }

    /**
     * 根据id结束营销活动
     * @return ItemWriter
     */
    public ItemWriter<Activity> finishMarketingActivity() {
        return list -> batchMapper.finishMarketingActivity((List<Activity>) list, PublishStatusEnum.END_STATUS.getResultCode());
    }

    /**
     * 根据id结束营销会议
     * @return ItemWriter
     */
    public ItemWriter<Conference> finishMarketingConference() {
        return list -> batchMapper.finishMarketingConference((List<Conference>) list, PublishStatusEnum.END_STATUS.getResultCode());
    }

    /**
     * 根据id发布/撤销课程
     * @return ItemWriter
     */
    public ItemWriter<Course> updateCourseStatus() {
        return list -> batchMapper.updateCourseStatus((List<Course>) list);
    }

    /**
     * 根据id发布/撤销考试
     * @return ItemWriter
     */
    public ItemWriter<Examination> updateExaminationStatus() {
        return list -> batchMapper.updateExaminationStatus((List<Examination>) list);
    }
}


